package ar.fiuba.tp7552.buildadmin.service.impl;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;

import ar.fiuba.tp7552.buildadmin.dao.UserDao;

public class AuthenticationServiceImpl implements UserDetailsService {
	
	private UserDao userDao;
	
	public AuthenticationServiceImpl(UserDao userDao) {
		this.userDao=userDao;
	}

	@Override
	@Transactional
	public UserDetails loadUserByUsername(String username) {
		try {
			UserDetails ud = userDao.findByUsername(username);
			if (ud == null) {
				throw new UsernameNotFoundException("No matching account");
			}
			return ud;
		} catch (UsernameNotFoundException e) {
			throw e;			
		} catch (Exception e) {
			throw new UsernameNotFoundException("No matching account", e);
		}
	}
}